package com.serookie.controller;

import com.serookie.entity.GoodsCategory;
import com.serookie.entity.Result;
import com.serookie.service.GoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author kevintam
 * @version 1.0
 * @date 2021/5/8 15:43
 */
@RestController
@Api(value="商品分类接口",tags={"商品分类接口"})
@RequestMapping("/admin/category")
public class GoodsCategoryController{

    @Autowired
    private GoodsCategoryService goodsTypeService;

    @GetMapping("/all")
    @ApiOperation("查询所有的子分类,以树形结构组装起来")
    public Result all(){
        List<GoodsCategory> all = goodsTypeService.getTypeWithTree();
        if(all!=null){
            return Result.ok().message("data").data("all",all);
        }
        return Result.error().message("访问失败");
    }

    /**
     * @requestBody: 获取请求体,必须发送post请求,因为只有post才有请求体
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("删除商品分类")
    public Result delete(@RequestBody Integer[] ids){
        goodsTypeService.removeByIds(Arrays.asList(ids));
        return Result.ok().message("删除成功");
    }
    @GetMapping("/count")
    @ApiOperation("查询所有的商品记录数")
    public Result count(){
        Integer integer = goodsTypeService.goodsCount();
        return Result.ok().message("操作成功").data("count",integer);
    }
    /**
     * 新增商品分类
     * @param category
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增分类")
    public Result save(@RequestBody GoodsCategory category){
        boolean save = goodsTypeService.save(category);
        if(save){
            return Result.ok().message("新增完成");
        }
        return Result.error().message("新增失败");
    }

    /**
     * 修改商品分类
     * @param category
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改商品分类")
    public Result update(@RequestBody GoodsCategory category){
        boolean flag = goodsTypeService.updateById(category);
        if(flag){
            return Result.ok().message("修改成功");
        }
        return Result.error().message("修改失败");
    }
    @GetMapping("/info/{id}")
    @ApiOperation("根据id查询商品")
    public Result info(@PathVariable("id") Integer id){
      GoodsCategory category= goodsTypeService.getById(id);
      return Result.ok().message("查询成功").data("category",category);
    }
}
